package com.graduation.smartparkingplatform.ServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.graduation.smartparkingplatform.Dao.CompanyMapper;
import com.graduation.smartparkingplatform.Pojo.Company;
import com.graduation.smartparkingplatform.Service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

@Service
public class CompanyServiceImpl implements CompanyService {
  @Autowired CompanyMapper companyMapper;

  @Override
  // 获取Company列表（分页）
  public PageInfo<Company> getCompanyList(Integer page, Integer count) {
    PageHelper.startPage(page, count);

    List<Company> companyList = companyMapper.getCompanyList();

    PageInfo<Company> pageInfo = new PageInfo<>(companyList);

    return pageInfo;
  }

  // 模糊搜索User（分页）
  @Override
  public PageInfo<Company> searchCompany(
      Integer page, Integer count, String companyName, String companyDirector) {
    PageHelper.startPage(page, count);

    List<Company> companyList = companyMapper.searchCompany(companyName, companyDirector);

    PageInfo<Company> pageInfo = new PageInfo<>(companyList);

    return pageInfo;
  }

  // 添加公司
  @Override
  public Boolean addCompany(Company company) {
    String companyName = company.getCompany_name();
    String companyDirector = company.getCompany_director();
    String telphone = company.getCompany_tel();
    String email = company.getCompany_email();
    // 获取当前时间
    Calendar calendar = Calendar.getInstance();
    // 定义时间格式化
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String nowTime = formatter.format(calendar.getTime());
    return companyMapper.addCompany(companyName, companyDirector, telphone, email, nowTime);
  }

  // 修改合作单位状态
  @Override
  public Boolean changeCompanyStatus(Integer companyId, Boolean status) {
    return companyMapper.changeCompanyStatus(companyId, status);
  }

  // 修改合作单位信息
  @Override
  public Boolean changeInfo(Company company) {
    Integer companyId = company.getCompany_id();
    String companyName = company.getCompany_name();
    String companyDirector = company.getCompany_director();
    String companyTel = company.getCompany_tel();
    String email = company.getCompany_email();
    // 获取当前时间
    Calendar calendar = Calendar.getInstance();
    // 定义时间格式化
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String nowTime = formatter.format(calendar.getTime());
    return companyMapper.changeInfo(
        companyId, companyName, companyDirector, companyTel, email, nowTime);
  }

  // 删除合作单位
  @Override
  public Boolean deleteCompany(Integer companyId) {
    return companyMapper.deleteCompany(companyId);
  }
}
